package com.pyg.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pyg.manager.service.BrandService;
import com.pyg.pojo.TbBrand;
import com.pyg.utils.PageResult;
import com.pyg.utils.PygResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by on 2018/8/10.
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Reference(timeout = 1000000)
    private BrandService brandService;

    /**
     * 需求：查询所有品牌
     */
    @RequestMapping("/findAll")
    public List<TbBrand> findAll() {
        //调用远程服务对象方法
        List<TbBrand> brandList = brandService.findAll();
        return brandList;

    }
    @RequestMapping("findByPage/{page}/{rows}")
    public PageResult findByPage(@PathVariable Integer page, @PathVariable Integer rows) {
        //调用远程服务对象方法
        PageResult result = brandService.findByPage(page,rows);
        return result;
    }
    @RequestMapping("add")
    public PygResult add(@RequestBody TbBrand tbBrand) {
        //调用远程服务对象方法
        try {
            brandService.add(tbBrand);
            return new PygResult(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "增加失败");
        }
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public TbBrand add(Long id) {
        //调用远程服务对象方法
        return brandService.findOne(id);
    }
    @RequestMapping("/update")
    public PygResult update(@RequestBody TbBrand tbBrand) {
        //调用远程服务对象方法
        try {
            brandService.update(tbBrand);
            return new PygResult(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "更新失败");
        }
    }
    @RequestMapping("/del/{ids}")
    public PygResult del(@PathVariable Long[] ids) {
        //调用远程服务对象方法
        try {
            brandService.del(ids);
            return new PygResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "删除失败");
        }
    }
    @RequestMapping("/findBrandSelect2List")
    public List<Map> findBrandSelect2List() {
        //调用远程服务对象方法
       return brandService.findBrandSelect2List();

    }
}
